package com.gourd.web.controller;

import com.gourd.common.annotation.NoRepeatSubmit;
import com.gourd.common.annotation.RateLimit;
import com.gourd.common.data.BaseResponse;
import com.gourd.common.exception.ServiceException;
import com.gourd.web.feign.RemoteClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author gourd
 */
@Api(tags = "smallGourd", description = "测试控制器")
@RestController
@RequestMapping("/gourd")
@Slf4j
public class SmallGourdController {

    @Autowired
    private RemoteClient remoteClient;


    @ApiOperation(value="gourd-feign-client测试Get接口")
    @GetMapping("/feign-client-get")
    public BaseResponse getTest() {
        return remoteClient.testServerG();
    }

    @ApiOperation(value="gourd-feign-client测试Post接口")
    @PostMapping("/feign-client-post")
    @ApiImplicitParams({@ApiImplicitParam(name = "jwt-token", value = "jwt-token", required = true, dataType = "string", paramType = "header")})
    public BaseResponse getPost() {
        return remoteClient.testServerP();
    }

    @ApiOperation(value="smallGourd测试接口")
    @GetMapping("/test")
    public BaseResponse gourdTest() {
        return BaseResponse.ok("smallGourd测试接口 success");
    }

    @ApiOperation(value="gourd-测试Get接口")
    @GetMapping("/testG")
    public BaseResponse gourdTestG() {
        // 测试熔断、降级
//        try {
//            Thread.sleep(500);
//        } catch (InterruptedException e) {
//            Thread.interrupted();
//            log.error("{}",e);
//        }
//        if(true){
//            throw new ServiceException("测试熔断");
//        }
        return BaseResponse.ok("gourd-feign-client测试Get接口：success");
    }

    @ApiImplicitParams({@ApiImplicitParam(name = "jwt-token", value = "jwt-token", required = true, dataType = "string", paramType = "header")})
    @ApiOperation(value="gourd-测试Post接口")
    @PostMapping("/testP")
    public BaseResponse  gourdTestP() {
        return BaseResponse.ok("gourd-feign-client测试Post接口：success");
    }

    @PostMapping("/repeat")
    @NoRepeatSubmit
    @ApiOperation(value="测试重复提交")
    public BaseResponse testRepeatSubmit() {
        return BaseResponse.ok("测试重复提交程序逻辑返回");
    }


    @RateLimit(limitNum = 5.0)
    @PostMapping("/rate-limit")
    @ApiOperation(value="测试限流")
    public BaseResponse testRateLimit() {
        return BaseResponse.ok("测试限流程序逻辑返回");
    }
}
